package com.jianshuge.app.bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jianshuge.app.AppException;
import com.jianshuge.app.bean.MyBooklistDetail.MyBooklistItem;
import com.jianshuge.app.bean.MyBooklistDetail.Reply;
import com.jianshuge.app.common.StringUtils;

/**
 * 数据操作结果实体类
 */
public class MyResult extends Base {

	private int errorCode;
	private String errorMessage;
	private String extraCode;
	
	public boolean OK() {
		return errorCode == 1;
	}

	/**
	 * 解析调用结果
	 * @param stream
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public static MyResult parse(InputStream inputStream) throws IOException, AppException {
		MyResult res = new MyResult();
		
		String inputStr;
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(inputStream) ); 
	    StringBuilder responseStrBuilder = new StringBuilder();
	    while ((inputStr = streamReader.readLine()) != null)
	        responseStrBuilder.append(inputStr);
	    String strResult = responseStrBuilder.toString();
	    
		try {
			
			JSONObject result_json_obj = new JSONObject(strResult);		
			res.errorCode 		= result_json_obj.optInt("error_code");
			res.errorMessage 	= result_json_obj.optString("error_message");
			res.extraCode 		= result_json_obj.optString("extra_code");
			
			res.setNotice(new Notice());
			res.getNotice().setAtmeCount( 0 );
	        res.getNotice().setMsgCount( StringUtils.toInt(res.extraCode,0) );
	        res.getNotice().setReviewCount( 0 );
	        res.getNotice().setNewFansCount( 0 );
			
		} catch (JSONException e) { 
			
            System.out.println("Jsons parse error !"); 
            e.printStackTrace(); 
        
		}
		

        
 		return res;
	}

	public int getErrorCode() {
		return errorCode;
	}
	
	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}
	
	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	
	public String getExtraCode() {
		return extraCode;
	}
	
	public void setExtraCode(String extra_code) {
		this.extraCode = extra_code;
	}

	public String toString(){
		return String.format("RESULT: CODE:%d,MSG:%s", errorCode, errorMessage);
	}
}
